﻿using System.Collections.Generic;
using UnityEngine;
using XLua;

public class PanelUtil : IUtil
{
    // Panel固定配置路径
    private const string PANEL_ASSET_PATH = "UIPrefab";
    // 缓存所有加载过的容器
    private Dictionary<PanelTypeEnum, GameObject> _cachePanelDic;
    // 依赖工具项
    GameLauncher launcher = null;
    AssetUtil assetUtil = null;

    // 层级节点
    public Canvas UICanvas = null;
    private Transform _topPanelTrans = null;
    private Transform _popPanelTrans = null;
    private Transform _businessPanelTrans = null;

    // 通用UI缓存
    public TopLoadingPanel topLoadingPanel = null;

    // 全局存在的通用UI

    public void Init()
    {
        _cachePanelDic = new Dictionary<PanelTypeEnum, GameObject>();

        launcher = GameLauncher.instance;
        assetUtil = launcher.assetUtil;

        // 初始化Canvas
        Transform canvasTrans = launcher.transform.Find("UICanvas");
        if (canvasTrans != null)
        {
            UICanvas = canvasTrans.GetComponent<Canvas>();
            _topPanelTrans = UICanvas.transform.Find("TopPanels");
            _popPanelTrans = UICanvas.transform.Find("PopPanels");
            _businessPanelTrans = UICanvas.transform.Find("BusinessPanels");
        }
        else
        {
            Debug.LogError($"canvas not exist!");
        }

        // 初始化常用UI
        ShowPanelByEnum(PanelTypeEnum.TopLoading);
        if (_cachePanelDic.TryGetValue(PanelTypeEnum.TopLoading, out GameObject loadingPanel))
        {
            topLoadingPanel = loadingPanel.GetComponent<TopLoadingPanel>();
        }

        Debug.Log("panel inited!");
    }

    public void Tick()
    {
        // ...
    }

    public void Uninit()
    {
        Debug.Log("panel uninited!");
    }
    /// <summary>
    /// 通过类型弹出对应面板
    /// </summary>
    /// <param name="type"></param>
    [LuaCallCSharp]
    public void ShowPanelByEnum(PanelTypeEnum type)
    {
        if (_cachePanelDic.TryGetValue(type, out GameObject panel))
        {
            panel.SetActive(true);
            return;
        }
        string resPath = $"{PANEL_ASSET_PATH}/{type}";
        GameObject newPanel = assetUtil.LoadPrefab(resPath);
        if (newPanel != null)
        {
            RectTransform rect = newPanel.GetComponent<RectTransform>();
            rect.SetParent(GetPanelParent(type));
            newPanel.SetActive(true);
            rect.anchoredPosition = Vector3.zero;
            rect.sizeDelta = Vector3.zero;
            _cachePanelDic.Add(type, newPanel);
        }
    }
    /// <summary>
    /// 通用弹出加载界面接口
    /// </summary>
    /// <param name="progress"></param>
    [LuaCallCSharp]
    public void ShowLoadingPanel(float progress)
    {
        topLoadingPanel.SetActiveAndProgress(progress != 1, progress);
    }

    // 获取UI预制体的对应父节点
    Transform GetPanelParent(PanelTypeEnum type)
    {
        string typeName = type.ToString().Substring(0, 3);
        switch (typeName)
        {
            case "Top":
                return _topPanelTrans;
            case "Pop":
                return _popPanelTrans;
            case "Bus":
                return _businessPanelTrans;
        }
        return null;
    }
}